﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Controls;
using Csla.Core;
using System.Collections;
using OpenExpressApp.WPF.Editors;
using System.Windows.Data;
using System.ComponentModel;
using OpenExpressApp.MetaModel;
using Csla;
using System.Reflection;
using OpenExpressApp.Types;
using System.Windows.Input;
using System.Windows;

namespace OpenExpressApp.Module.WPF.Editors
{
    /// <summary>
    /// 列表编辑器
    /// </summary>
    public abstract class ListEditor
    {
        private IListObjectView _view;

        private Type _bOType;

        private FrameworkElement _control;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="listview">视图对象</param>
        /// <param name="boType">显示的业务模型类型</param>
        public ListEditor(IListObjectView listview, Type boType)
        {
            this._view = listview;
            this._bOType = boType;
        }

        /// <summary>
        /// 视图对象
        /// </summary>
        protected IListObjectView View
        {
            get
            {
                return this._view;
            }
        }

        /// <summary>
        /// 显示的业务模型类型
        /// </summary>
        public Type BOType
        {
            get
            {
                return this._bOType;
            }
        }

        /// <summary>
        /// 列表编辑控件
        /// </summary>
        public FrameworkElement Control
        {
            get
            {
                if (this._control == null)
                {
                    this._control = this.CreateControl();
                }
                return this._control;
            }
        }

        private bool? _isReadOnly;
        /// <summary>
        /// 编辑器是否只读的
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return this._isReadOnly.GetValueOrDefault(
                    //不在数据区域 或 模型标记不可编辑
                    ApplicationModel.GetBusinessObjectInfo(this._view.EntityType).NotAllowEdit ||
                        (this._view.Data is IBindingList && (this._view.Data as IBindingList).AllowEdit == false)
                    );
            }
            set
            {
                this._isReadOnly = value;
            }
        }

        /// <summary>
        /// 当前选中的数据模型
        /// </summary>
        public abstract object CurrentObject { get; set; }

        /// <summary>
        /// 当前选中的数据模型集合
        /// </summary>
        public abstract IList SelectedObjects { get; }

        /// <summary>
        /// 通知列表编辑器，View.Data已经被改变了。
        /// </summary>
        public abstract void NotifyViewDataChanged();

        /// <summary>
        /// 选择所有的数据
        /// </summary>
        public abstract void SelectAll();

        protected abstract FrameworkElement CreateControl();
    }
}
